//
//  MTInfoVerificationGenderCell.swift
//  MT
//
//  Created by M on 2019/12/22.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

@objc
protocol MTInfoVerificationGenderCellDelegate: NSObjectProtocol {
    /// 选择性别回调
    @objc optional func genderCell(_ cell: MTInfoVerificationGenderCell, didSelectGender gender: LocalGenderModel.GenderType)
}

class MTInfoVerificationGenderCell: BaseMTInfoVerificationCell {
    
    /// 代理
    weak var delegate: MTInfoVerificationGenderCellDelegate?
    
    var selectedType: LocalGenderModel.GenderType? {
        didSet {
            guard let type = selectedType else { return }
            maleButton.isSelected = type == .male
            femaleButton.isSelected = type == .female
        }
    }
    
    /// 男按钮
    lazy var maleButton: UIButton = {
        let btn = UIButton()
        btn.tag = 0
        btn.setImage(UIImage(named: "sex-man-default"), for: .normal)
        btn.setImage(UIImage(named: "sex-man-selected"), for: .selected)
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(genderCell(didClickGenderButton:)), for: .touchUpInside)
        return btn
    }()
    
//    /// 男文字
//    lazy var maleLabel: UILabel = {
//        let label = UILabel()
//        label.text = "男士"
//        label.font = PingFangSCMediumFont(12)
//        label.textColor = .white
//        label.textAlignment = .center
//        return label
//    }()
    
    /// 女按钮
    lazy var femaleButton: UIButton = {
        let btn = UIButton()
        btn.tag = 1
        btn.setImage(UIImage(named: "sex-woman-default"), for: .normal)
        btn.setImage(UIImage(named: "sex-woman-selected"), for: .selected)
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(genderCell(didClickGenderButton:)), for: .touchUpInside)
        return btn
    }()
    
//    /// 女文字
//    lazy var femaleLabel: UILabel = {
//        let label = UILabel()
//        label.text = "女士"
//        label.font = PingFangSCMediumFont(12)
//        label.textColor = .white
//        label.textAlignment = .center
//        return label
//    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MTInfoVerificationGenderCell {
    
    private func initializeUI() {
        contentView.addSubview(maleButton)
//        contentView.addSubview(maleLabel)
        contentView.addSubview(femaleButton)
//        contentView.addSubview(femaleLabel)
        
        maleButton.snp.makeConstraints { (make) in
            make.right.equalTo(contentView.snp.centerX).offset(-25)
            make.centerY.equalTo(titleLabel)
            make.width.equalTo(80)
            make.height.equalTo(32)
        }
        
//        maleLabel.snp.makeConstraints { (make) in
//            make.centerX.equalTo(maleButton)
//            make.top.equalTo(maleButton.snp.bottom).offset(6)
//        }
        
        femaleButton.snp.makeConstraints { (make) in
            make.left.equalTo(contentView.snp.centerX).offset(25)
            make.centerY.equalTo(titleLabel)
            make.width.equalTo(80)
            make.height.equalTo(32)
        }
        
        textField.snp.remakeConstraints { (make) in
            make.centerY.equalTo(titleLabel).offset(40)
            make.left.equalTo(maleButton)
        }
        
        underline.snp.remakeConstraints { (make) in
            make.top.equalTo(textField.snp.bottom).offset(10)
            make.left.equalTo(contentView).offset(20)
            make.right.equalTo(contentView).offset(-20)
            make.height.equalTo(0.5)
        }
        
//        femaleLabel.snp.makeConstraints { (make) in
//            make.centerX.equalTo(femaleButton)
//            make.top.equalTo(maleLabel)
//        }
    }
    
}

// MARK: ------------------ 事件方法 ----------------------
@objc
extension MTInfoVerificationGenderCell {

    private func genderCell(didClickGenderButton sender: UIButton) {
        maleButton.isSelected = sender.tag == 0
        femaleButton.isSelected = sender.tag == 1
        if let delegate = delegate, let type = LocalGenderModel.GenderType(rawValue: sender.tag) {
            delegate.genderCell?(self, didSelectGender: type)
        }
    }
    
}
